package cn.itcast.Service.impl;

import cn.itcast.Service.TeacherService;
import cn.itcast.common.PageBean;
import cn.itcast.domain.dto.PageDTO;
import cn.itcast.domain.dto.TeacherDTO;
import cn.itcast.domain.vo.TeacherVO;
import cn.itcast.mapper.TeacherMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service //标注在实现类
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public PageBean getTeacherList(PageDTO pageDTO) {
        Integer pageNum = pageDTO.getPageNum();
        Integer pageSize = pageDTO.getPageSize();
        // 设置分页信息
        PageHelper.startPage(pageNum, pageSize);
        // 调用mapper中的分页查询方法
        List<TeacherVO> teacherList = teacherMapper.getTeacherList();
        /*
        * 可以把数据强转为Page或者PageInfo，两者都可以拿到分页数据
        * PageInfo拿到的分页参数比Page更多，比如上一页、下一页等等
        * Page是一个List,PageInfo有List构造
        * 根据前端要求选择两者之一
        * */
        Page p = (Page) teacherList;
        PageBean pageBean = new PageBean(p.getTotal(),p.getResult());
        return pageBean;
    }

    @Override
    public int addTeacher(TeacherDTO teacher) {
        int i = teacherMapper.addTeacher(teacher);
        return i;
    }

    @Override
    public int deleteTeacherById(Integer teacherId) {
        int i = teacherMapper.deleteTeacherById(teacherId);
        return i;
    }

    @Override
    public int updateTeacher(TeacherDTO teacher) {
        int i = teacherMapper.updateTeacher(teacher);
        return i;
    }
}
